package com.cy.service.impl;

import com.cy.entity.Member;
import com.cy.entity.SysConfig;
import com.cy.entity.Team;
import com.cy.service.SysConfigService;
import com.cy.service.TeamService;
import com.cy.utils.StringUtil;
import com.cy.vo.MemberVo;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.cy.dao.MemberDao;
import com.cy.service.MemberService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;


@Service("memberService")
@Transactional
public class MemberServiceImpl implements MemberService {
	@Autowired
	private MemberDao memberDao;
	@Autowired
	private TeamService teamService;
	@Autowired
	private SysConfigService configService;
	
	@Override
	public Member queryObject(Long id){
		return memberDao.queryObject(id);
	}
	
	@Override
	public List<Member> queryList(Map<String, Object> map){
		return memberDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return memberDao.queryTotal(map);
	}
	
	@Override
	public void save(Member member){
		memberDao.save(member);
	}
	
	@Override
	public void update(Member member){
		memberDao.update(member);
	}
	
	@Override
	public void delete(Long id){
		memberDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Long[] ids){
		memberDao.deleteBatch(ids);
	}

	@Override
	public List<Member> findByCode(String recommendCode) {
		return memberDao.findByCode(recommendCode);
	}
	@Override
	public List<Member> findByTeamCode(String teamCode) {
		return memberDao.findByTeamCode(teamCode);
	}

	@Override
	public void saveMemberAndTeam(Member member) {
		//保存会员信息
		save(member);
		//保存团队信息
		Team team=new Team();
		team.setTeamCode(member.getTeamCode());
		team.setInitAccount(member.getAccount());
		team.setInitPhone(member.getPhone());
		team.setInitName(member.getName());
		team.setTeamNumber(1);
		team.setCreateTime(new Date());
		teamService.save(team);
	}

	@Override
	public List<Member> findByAccount(String account) {
		return memberDao.findByAccount(account);
	}

	@Override
	public List<MemberVo> exportList() {
		return memberDao.exportList();
	}

	@Override
	public void updateState(Long[] ids,String stateValue) {
		for (long id:ids){
			Member member =queryObject(id);
			member.setState(stateValue);
			update(member);
		}
	}

	@Override
	public void importXls(MultipartFile file) {
		List<SysConfig> configs=configService.findByCode("cfg_default_coin");
		int defaultCoin=10000;
		if(configs!=null&&configs.size()>0){
			defaultCoin=Integer.parseInt(configs.get(0).getValue());
		}
		try {
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			// 创建对Excel工作簿文件的引用
			HSSFWorkbook wookbook = new HSSFWorkbook(file.getInputStream());
			// 在Excel文档中，第一张工作表的缺省索引是0
			// 其语句为：HSSFSheet sheet = workbook.getSheetAt(0);­
			HSSFSheet sheet = wookbook.getSheet(file.getOriginalFilename());
			//获取到Excel文件中的所有行数­
			int rows = sheet.getPhysicalNumberOfRows();
			//遍历行­
			for (int i = 1; i < rows; i++) {
				// 读取左上端单元格­
				HSSFRow row = sheet.getRow(i);
				// 行不为空­
				if (row != null) {
					//获取到Excel文件中的所有的列­
					int cells = row.getPhysicalNumberOfCells();
					String value = "";
					//遍历列­
					for (int j = 0; j < cells; j++) {
						//获取到列的值­
						HSSFCell cell = row.getCell(j);
						if (cell != null) {
							switch (cell.getCellType()) {
								case HSSFCell.CELL_TYPE_FORMULA:
									break;
								case HSSFCell.CELL_TYPE_NUMERIC:
									if (HSSFDateUtil.isCellDateFormatted(cell)) {
										//  如果是date类型则 ，获取该cell的date值
										Date date=HSSFDateUtil.getJavaDate(cell.getNumericCellValue());
										String  str = sdf.format(date);
										value = str+ ",";
									} else {
										cell.toString();
										value += cell.getNumericCellValue() + ",";
									}
									break;
								case HSSFCell.CELL_TYPE_STRING:
									value += cell.getStringCellValue() + ",";
									break;
								default:
									value += ",";
									break;
							}
						}
					}
			     	// 更新已充值云币
					String[] val = value.split(",");
					String account=val[0];
					String getCoin=StringUtil.isEmpty(val[11])?"0":val[11];
					if(getCoin.endsWith(".0")){
						getCoin=getCoin.substring(0,getCoin.length()-2);
					}
					List<Member> memberList = findByAccount(account);
					if(memberList!=null&&memberList.size()>0){
						memberList.get(0).setGetCoin(Long.parseLong(getCoin));
						update(memberList.get(0));
					}
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	@Override
	public void importXlsx(MultipartFile file) {
//		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		try {
//			// 创建对Excel工作簿文件的引用
//			XSSFWorkbook wookbook = new XSSFWorkbook(file.getInputStream());
//			// 在Excel文档中，第一张工作表的缺省索引是0
//			// 其语句为：HSSFSheet sheet = workbook.getSheetAt(0);­
//			XSSFSheet sheet = wookbook.getSheet("Sheet1");
//			//获取到Excel文件中的所有行数­
//			int rows = sheet.getPhysicalNumberOfRows();
//			//遍历行­
//			for (int i = 1; i < rows; i++) {
//				// 读取左上端单元格­
//				XSSFRow row = sheet.getRow(i);
//				// 行不为空­
//				if (row != null) {
//					//获取到Excel文件中的所有的列­
//					int cells = row.getPhysicalNumberOfCells();
//					String value = "";
//					//遍历列­
//					for (int j = 0; j < cells; j++) {
//						//获取到列的值­
//						XSSFCell cell = row.getCell(j);
//						if (cell != null) {
//							switch (cell.getCellType()) {
//								case HSSFCell.CELL_TYPE_STRING:
//									value += cell.getStringCellValue() + ",";
//									break;
//								case HSSFCell.CELL_TYPE_FORMULA:
//									break;
//								case HSSFCell.CELL_TYPE_NUMERIC:
//									cell.toString();
//									value += cell.getNumericCellValue() + ",";
//									break;
//								default:
//									value += "0";
//									break;
//							}
//						}
//					}
//					// 将数据插入到mysql数据库中­
//					String[] val = value.split(",");
//					String teamCode= StringUtil.isEmpty(val[2])?"-":val[2];
//					String isInit=StringUtil.isEmpty(val[6])?"0":val[6];
//					String state="1".equals(isInit)?"0":"1";
//					String recommendCode=StringUtil.isEmpty(val[1])?"-":val[1];
//					Member member = new Member();
//					member.setAccount(val[3]);
//					member.setName(val[4]);
//					member.setPhone(val[5]);
//					member.setTeamCode(teamCode);
//					member.setState(state);
//					member.setRecommendCode(recommendCode);
//					member.setIsInit(isInit);
//					member.setRecommendLowerNumber(0);
//					member.setTeamParentAccount("-");
//					member.setRecommendAccount("-");
//					member.setPayAmount("-");
//					try {
//						member.setPayTime(sdf.parse(val[0]));
//					} catch (ParseException e) {
//						e.printStackTrace();
//					}
//
//					save(member);
//				}
//			}
//		} catch (FileNotFoundException e) {
//			e.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//
	}
}
